<?php

namespace app\controllers;

use yii\web\Controller;
use Yii;
use app\models\content;
use app\models\login;
use app\models\like;

class ContentController extends Controller{
	  public $enableCsrfValidation = false;
	  //信息发布
	  public function actionGet(){
		  $model = new content();
		  if(Yii::$app->request->isPost){
		      $postData = Yii::$app->request->bodyParams;
		      $first = $_POST['first'];
		      $last = $_POST['last'];
		      $len = $last - $first;
		      $sql = "select * from wall_content where state = 1 order by content_id desc limit $first,$len";
		      $data = content::findBysql($sql)->asArray()->all();
		      echo json_encode(array('error' => 0, 'message' =>'success','data' => $data,));
		  }
               exit(0);
	  }
	  
	  //信息接收&&数据库输入
	  public function actionPublish(){
		  $model = new content();
                  if(Yii::$app->request->isPost ){
			$postData = Yii::$app->request->post();
                        $result=$model->add($postData);
                        if( $result == 0 ){
        		$data = array('Content' => '提交数据成功');
        		echo json_encode(array('error' => 0, 'message' => 'success', 'data' => $data));
        	}else{
        		$data = array('content' => '提交数据失败,请@留言,联系公众号~', 'Content' => $result);
             	        echo json_encode(array('error' => 1, 'message' =>'fail', 'data' => $data));
        	}   
       } 
           exit(0);
    }        

             public function actionUpuser(){
                    $user = new login();
                    if(Yii::$app->request->isPost ){
                         $postData = Yii::$app->request->post();
                         $result = $user->mod($postData);
                         if( $result == 0){
                            $data  = array('content' => '更新成功');
                            echo json_encode(array('error' => 0, 'message' => 'success', 'data' => $data));
           }else{
                            $data = array('Content' => '更新失败','content' => $result);
                            echo json_encode(array('error' => 1, 'message' => 'fail', 'data' => $data));
                    }
           } 
             exit(0);
      }
             
              //获取特定用户的所有发表过的内容
              public function actionRecord(){
		    $model = new content();
		    if(Yii::$app->request->isPost ){
			$postData = Yii::$app->request->post();
			$openid = $postData['username'];
                        $sql = "select * from wall_content where openid = '$openid' and state = 1 order by content_id desc";
					
			$data = content::findBysql($sql)->asArray()->all();
			echo json_encode(array('error' => 0, 'message' =>'success','data' => $data,));
		  } 
                       exit(0);
	   } 
		 
		 //点赞(数)&&like表 content表数据库输入
	 public function actionLikerender(){
	         $model = new content();
                 $like = new like();
	         if(Yii::$app->request->isPost ){
		    $postData = Yii::$app->request->post();
                    $content_id = $postData['formid'];
                    $likecount = $postData['dzSum'];
                     //查找是否存在此内容id
                    $c_arr= $model->find()->select('*')->where('state = :state',[":state"=>'1'])->andwhere('content_id = :content_id',[":content_id"=>$content_id])->asArray()->all();
                    if(empty($c_arr)){
                       $data = array('content' => '点赞失败');
                       echo json_encode(array('error' => -1, 'message' => 'fail', 'data' => $data));
                     } else{
                       $modcontent = array('likecount' =>$likecount);
                       $model->mod($content_id,$modcontent);
                       $data = array('content' =>'点赞成功', 'count_p' => $likecount);
                       echo json_encode(array('error' => 0, 'message' => 'success', 'data' => $data));
                    }
                       //数据库(like表)输入
                       $result=$like->add($postData);
                 /**      if( $result == 0 ){
                          $data = array('Content' => '点赞内容渲染成功');
                          echo json_encode(array('error' => 0, 'message'=> 'success','data' => $data));
                      }else{
                          $data = array('content'=>'点赞内容渲染失败','Content'=> $result);
                          echo json_encode(array('error' => 1, 'message' =>'fail', 'data' => $data));
                } */
          }
                    exit(0);
     }
	     //点赞渲染：把该openid点过赞的内容id传给前端  
            public function actionShowlike(){
                    $zan = new like();
                    if(Yii::$app->request->isPost ){
                        $postData = Yii::$app->request->post();
                        $openid = $postData['username'];
                        $sql = "select * from wall_like where openid = '$openid'";
                        $data = like::findBySql($sql)->asArray()->all();
                        echo json_encode(array('error' => 0,'message' => 'success','data' => $data,));  
                  } 
                       exit(0);
         }
	
		 //删除内容
            public function actionDelete(){
                     $model = new content();
		     if(Yii::$app->request->isPost ){   
		        $postData = Yii::$app->request->post();
                        $result = $model->del($postData);
                        if($result == 0){
                           $data = array('content'=>'删除数据成功');
                           echo json_encode(array('error'=> 0,'message'=>'success','data' => $data)); 
                       }else{
                           $data = array('Content' => '提交数据失败,请@留言,联系公众号~','content' => $result);
                           echo json_encode(array('error' => 1, 'message' =>fail,'data' => $data));
                         }
                    }
                           exit(0);   
           }
               		 
	     public function actionRedischeck($IP){
		$redis = Yii::$app->redis->get($IP);//获取键为$IP的值
        	    if($redis){
        		$difftime  = time() - $redis;//对比时间差
        		if( $difftime > 7200 ){
        			Yii::$app->redis->set($IP, time());//更新$IP键值
        			$tip = 0;
        			return $tip;
        		}else{
        			$tip = -1;
        			return $tip;
        		} 
        	}else{
        		Yii::$app->redis->set($IP, time());//设置$IP键值
        		$tip = 1;
        		return $tip;
        	}
	}
		 
}
